package com.coincalf.task.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;


/**
 * 自定义币种，即新上币种信息生成
 */
@Slf4j
@Component
@EnableScheduling
public class CustomizeCoin {


    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * AQS价格随机变化 110-120
     */
    @Scheduled(fixedRate = 500L)
    public void setAqsPrice() {
        String redisKeyAqsCurrentPrice = "current:price:aqs";
        setPrice(redisKeyAqsCurrentPrice, BigDecimal.valueOf(110), BigDecimal.valueOf(110), BigDecimal.valueOf(120));
    }

    /**
     * AQP价格随机变化  80-90
     */
    @Scheduled(fixedRate = 500L)
    public void setAqpPrice() {
        String redisKeyAqpCurrentPrice = "current:price:aqp";
        setPrice(redisKeyAqpCurrentPrice, BigDecimal.valueOf(80), BigDecimal.valueOf(80), BigDecimal.valueOf(90));
    }

    /**
     * 价格随机变化
     *
     * @param redisKeyAqpCurrentPrice 缓存key
     * @param initPrice               初始价格
     * @param lowestPrice             最低价
     * @param highestPrice            最高价
     */
    private void setPrice(String redisKeyAqpCurrentPrice, BigDecimal initPrice, BigDecimal lowestPrice, BigDecimal highestPrice) {
        BigDecimal currentPrice = (BigDecimal) redisTemplate.opsForValue().get(redisKeyAqpCurrentPrice);
        if (currentPrice == null) {
            //初始化价格 1USDT
            currentPrice = initPrice;
        } else {
            /**
             * TODO 如果设置了价格走势 则按照走势走 否则价格随机变动
             * 随机变动规则：1：涨或者跌  2：变化幅度
             */
            /**
             * 随机涨跌 0跌 1涨
             */
            int upOrDown;
            if (currentPrice.compareTo(highestPrice) > 0) {
                upOrDown = 0;
            } else if (currentPrice.compareTo(lowestPrice) < 0) {
                upOrDown = 1;
            } else {
                upOrDown = CustomizeCoin.upOrDown();
            }
            /**
             * 变化幅度
             * 0-0.5 60%概率
             * 0.5-1 30%概率
             * 1-1.5 5%概率
             * 1.5-2 3%概率
             * 2-3   2%概率
             */
            BigDecimal increaseRange = CustomizeCoin.increaseRange().divide(BigDecimal.valueOf(100), 8, RoundingMode.DOWN);
            if (upOrDown == 0) {
                increaseRange = increaseRange.negate();
            }
            currentPrice = currentPrice.multiply(BigDecimal.ONE.add(increaseRange));
        }
        redisTemplate.opsForValue().set(redisKeyAqpCurrentPrice, currentPrice.setScale(8, RoundingMode.DOWN));
    }

    public static void main(String[] args) {
        BigDecimal lowestPrice=BigDecimal.valueOf(80);
        BigDecimal highestPrice=BigDecimal.valueOf(90);
        BigDecimal[] arr = new BigDecimal[5000];
        BigDecimal currentPrice = BigDecimal.ONE;
        for (int i = 0; i < 5000; i++) {
            int upOrDown;
            if (currentPrice.compareTo(highestPrice) > 0) {
                upOrDown = 0;
            } else if (currentPrice.compareTo(lowestPrice) < 0) {
                upOrDown = 1;
            } else {
                upOrDown = CustomizeCoin.upOrDown();
            }
            /**
             * 变化幅度
             * 0-0.5 60%概率
             * 0.5-1 30%概率
             * 1-1.5 5%概率
             * 1.5-2 3%概率
             * 2-3   2%概率
             */
            BigDecimal increaseRange = CustomizeCoin.increaseRange().divide(BigDecimal.valueOf(100), 8, RoundingMode.DOWN);
            if (upOrDown == 0) {
                increaseRange = increaseRange.negate();
            }
            System.out.println("本次涨跌幅=" + increaseRange);
            currentPrice = currentPrice.multiply(BigDecimal.ONE.add(increaseRange));
            BigDecimal bigDecimal = currentPrice.setScale(4, RoundingMode.DOWN);
            System.out.println("当前价格=" + bigDecimal);
            arr[i] = bigDecimal;
        }
        for (BigDecimal b : arr) {
            System.out.print(b + ",");
        }

    }

    /**
     * 涨跌
     *
     * @return
     */
    private static int upOrDown() {
        int i = new Random().nextInt(10);
        if (i < 5) {
            return 0;
        }
        return 1;
    }

    /**
     * 随机计算价格涨跌幅度
     *
     * @return
     */
    private static BigDecimal increaseRange() {
        int result = new Random().nextInt(100);
        double range = new Random().nextDouble() / 2;
        if (result < 60) {
            //0-0.5
            return new BigDecimal(String.valueOf(range));
        } else if (result < 90) {
            //0.5-1
            return new BigDecimal(String.valueOf(range + 0.5));
        } else if (result < 95) {
            //1-1.5
            return new BigDecimal(String.valueOf(range + 1));
        } else if (result < 98) {
            //1.5-2
            return new BigDecimal(String.valueOf(range + 1.5));
        } else {
            //2-3
            return new BigDecimal(String.valueOf(range + 2));
        }
    }

}
